|
1
|
|
|
import ConnectionSettings from "./ConnectionSettings"; |
|
2
|
|
|
|
|
3
|
|
|
export default class ConnectionRequest { |
|
4
|
|
|
|
|
5
|
|
|
private connectionRequestSettings: ConnectionSettings; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
constructor(connectionRequestSettings: ConnectionSettings) { |
|
9
|
|
|
this.connectionRequestSettings = connectionRequestSettings; |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
all(model): Promise<unknown> { |
|
14
|
|
|
return fetch( |
|
15
|
|
|
`${this.modelApiLocation(model)}`, |
|
16
|
|
|
{ |
|
17
|
|
|
"method": 'GET', |
|
18
|
|
|
...this.connectionRequestSettings.getSettings() |
|
19
|
|
|
} |
|
20
|
|
|
); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
delete(model): Promise<unknown> { |
|
24
|
|
|
return fetch( |
|
25
|
|
|
`${this.modelApiLocation(model)}/${model.primaryKey}`, |
|
26
|
|
|
{ |
|
27
|
|
|
"method": 'DELETE', |
|
28
|
|
|
...this.connectionRequestSettings.getSettings(), |
|
29
|
|
|
} |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
get(model) { |
|
34
|
|
|
return fetch( |
|
35
|
|
|
`${this.modelApiLocation(model)}/${model.primaryKey}`, |
|
36
|
|
|
{ |
|
37
|
|
|
"method": 'GET', |
|
38
|
|
|
...this.connectionRequestSettings.getSettings(), |
|
39
|
|
|
} |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
modelApiLocation(model): string { |
|
44
|
|
|
return `${this.connectionRequestSettings.getBaseUrl()}/${model.kebabCaseClassName}`; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
patch(model):Promise<unknown> { |
|
48
|
|
|
return fetch( |
|
49
|
|
|
`${this.modelApiLocation(model)}/${model.primaryKey}`, |
|
50
|
|
|
{ |
|
51
|
|
|
"method": 'PATCH', |
|
52
|
|
|
"body": JSON.stringify(model.dirtyFields), |
|
53
|
|
|
...this.connectionRequestSettings.getSettings(), |
|
54
|
|
|
} |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
post(model):Promise<unknown> { |
|
59
|
|
|
return fetch( |
|
60
|
|
|
this.modelApiLocation(model), |
|
61
|
|
|
{ |
|
62
|
|
|
"method": 'POST', |
|
63
|
|
|
"body": model.jsonStringify(), |
|
64
|
|
|
...this.connectionRequestSettings.getSettings(), |
|
65
|
|
|
} |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
put(model):Promise<unknown> { |
|
70
|
|
|
return fetch( |
|
71
|
|
|
`${this.modelApiLocation(model)}/`, |
|
72
|
|
|
{ |
|
73
|
|
|
"method": 'PUT', |
|
74
|
|
|
"body": model.jsonStringify(), |
|
75
|
|
|
...this.connectionRequestSettings.getSettings(), |
|
76
|
|
|
} |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |